package org.jash.learnmvp.user;

import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;

import org.jash.learnmvp.mvp.BasePresenter;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.disposables.Disposable;

public class LoginPresenter extends BasePresenter<ConStruct.ILoginView> implements ConStruct.ILoginPresenter {
   private ConStruct.ILoginModel model = new LoginModel();
   @Override
   public void login(String username, String password) {
       EMClient.getInstance().login("abc", "123", new EMCallBack() {
           @Override
           public void onSuccess() {

           }

           @Override
           public void onError(int i, String s) {

           }
       });
      Disposable d = model.login(username, password)
              .observeOn(AndroidSchedulers.mainThread())
              .subscribe(res -> {
                  if (isAttach()) {
                     if (res.getCode() == 0) {
                        getWhoView().loginSuccess(res.getData());
                     } else {
                        getWhoView().loginFailure(new RuntimeException(res.getMsg()));
                     }
                  }
              }, t -> {
                  if (isAttach()) {
                     getWhoView().loginFailure(t);
                  }
              });

      disposable.add(d);
   }
}
